package cmq;

public class ThreadLocalExample {

    // SimpleDateFormat 不是线程安全的，所以每个线程都要有自己独立的副本

    public static void main(String[] args) throws InterruptedException {
        ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();
        Test test = new Test(100);
        for (int i = 0; i < 10; i++) {
            new Thread(test, "线程" + i).start();
        }
    }


    static class Test implements Runnable {
        ThreadLocal<Integer> integerThreadLocal = ThreadLocal.withInitial(() -> 10);

        public Test(int Local) {
            this.integerThreadLocal.set(Local);
        }

        @Override
        public void run() {
            String name = Thread.currentThread().getName();
            if ("线程1".equals(name)) {
                integerThreadLocal.set(100);
                integerThreadLocal.set(19);
            }
            try {
                Thread.sleep(1000);

            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(name + ":" + integerThreadLocal.get());
            System.out.println(name + ":" + integerThreadLocal.get());
        }
    }

}
